package com.hqyj.service.impl;

import com.hqyj.dao.EmployeeMapper;
import com.hqyj.pojo.Employee;
import com.hqyj.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    EmployeeMapper employeeMapper;

    public HashMap<String, Object> select(Employee employee, HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        UsernamePasswordToken token = new UsernamePasswordToken(employee.geteName(), employee.getePwd());
        //2  获取shrio用户对象
        Subject subject = SecurityUtils.getSubject();
        //3 登录
        try {
            subject.login(token);

            map.put("info", "登录成功！");

            Employee e = employeeMapper.select(employee);

            HttpSession session = request.getSession();
            session.setAttribute("user", e);

        } catch (UnknownAccountException e) {
            e.printStackTrace();
            map.put("info", "用户不存在！");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            map.put("info", "密码错误！");
        }
        return map;
    }
}
